package com.hehe.boot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hehe.boot.mapper.DoctorMapper;
import com.hehe.boot.pojo.Book;
import com.hehe.boot.pojo.Doctor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.print.Doc;
import java.time.LocalDateTime;

@Service
public class DoctorService extends ServiceImpl<DoctorMapper, Doctor> {

    private final BookService bookService;
    private static final Integer IDENTITY = 2;
    // 构造器注入
    public DoctorService(BookService bookService) {
        this.bookService = bookService;
    }

    @Transactional
    public Long register(String username, String password, Doctor doctor) {
        Book book = new Book();
        book.setIdentity(IDENTITY);
        book.setUsername(username);
        book.setPassword(password);
        this.save(doctor);
        book.setRelatedId(doctor.getId());
        this.bookService.save(book);
        doctor.setBookId(book.getId());
        this.updateById(doctor);
        return doctor.getId();
    }

    public Doctor showDetailById(Long doctorId){
        return this.getById(doctorId);
    }

    public Boolean setWorkTime(Long doctorId,LocalDateTime workStart, LocalDateTime workEnd,Integer workTimes) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", doctorId);
        updateWrapper.set("work_start",workStart);
        updateWrapper.set("work_end",workEnd);
        updateWrapper.set("work_times",workTimes);
        updateWrapper.set("remain",workTimes);
        return this.update(updateWrapper);
    }

    public Boolean updateDoctor(Doctor doctor) {
        UpdateWrapper<Doctor> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",doctor.getId());
        if (doctor.getName() != null){
            updateWrapper.set("name",doctor.getName());
        }
        if (doctor.getClinicId() != null){
            updateWrapper.set("clinic_id",doctor.getClinicId());
        }
        if (doctor.getWorkRoom() != null){
            updateWrapper.set("work_room",doctor.getWorkRoom());
        }
        if (doctor.getDegrees() != null){
            updateWrapper.set("degrees",doctor.getDegrees());
        }
        if (doctor.getImageUrl() != null){
            updateWrapper.set("image_url",doctor.getImageUrl());
        }
        if (doctor.getDetail() != null){
            updateWrapper.set("detail",doctor.getDetail());
        }
        return this.update(updateWrapper);
    }

    public IPage<Doctor> getProPageLike(String query, int currentPage) {
        // 构造分页条件
        Page<Doctor> page = new Page<>(currentPage, 10);

        // 构造查询条件
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", query);

        // 执行分页查询
        return page(page, queryWrapper);
    }

    public IPage<Doctor> getProPage(int currentPage){
        // 构造分页条件
        Page<Doctor> page = new Page<>(currentPage, 10);

        // 执行分页查询
        return this.page(page);
    }

    public boolean insertDoctor(Doctor product) {
        product.setId(0L);
        boolean result = this.save(product);
        return result;
    }

    public boolean deleteDoctor(Doctor product) {
        boolean result = this.removeById(product.getId());
        return result;
    }
}
